package top.liujiachen.springboot.shiro.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.liujiachen.springboot.shiro.dao.impl.GroupDao;
import top.liujiachen.springboot.shiro.dao.impl.PermissionDao;
import top.liujiachen.springboot.shiro.dao.impl.RoleDao;
import top.liujiachen.springboot.shiro.dao.impl.UserDao;
import top.liujiachen.springboot.shiro.entity.SysRole;
import top.liujiachen.springboot.shiro.entity.SysUser;
import top.liujiachen.springboot.shiro.service.ILoginService;
import top.liujiachen.springboot.shiro.vo.LoginUser;

import java.util.*;

@Service
public class LoginService implements ILoginService {

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

    @Override
    public LoginUser getByUsername(String username) {
        LoginUser user = null;
        SysUser sysUser = userDao.getByUsername(username);
        if (Optional.ofNullable(sysUser).isPresent()) {
//            SysGroup sysGroup = groupDao.getById(sysUser.getGroupId());
            SysRole role = roleDao.getById(sysUser.getRoleId());
            List<String> permissions = null;
            if (Optional.ofNullable(role).isPresent()) {
                permissions = permissionDao.getPermissionsByIds(role.getPermissions());
            }
            user = new LoginUser(String.valueOf(sysUser.getId()), sysUser.getUsername(), sysUser.getPassword(), sysUser.getNickname(), role.getCode(), permissions);
        }
        return user;
    }

}



